package com.ruoyi.mf178.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.mf178.domain.Mf178Order;
import com.ruoyi.mf178.domain.Mf178SubmitOrder;
import com.ruoyi.mf178.domain.Mf178SupplyGood;
import com.ruoyi.mf178.domain.Mf178TradeResponse;
import com.ruoyi.mf178.service.IMf178CouponTradeService;
import com.ruoyi.mf178.service.IMf178OrderService;
import com.ruoyi.mf178.utils.Mf178Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("mcd_xhl")
public class CouponTradeMcd implements IMf178CouponTradeService {

    @Value("${mcd_xhl.baseUrl}")
    private String baseUrl;
    @Value("${mcd_xhl.userName}")
    private String userName;
    @Value("${mcd_xhl.password}")
    private String password;

    @Autowired
    private IMf178OrderService mf178OrderService;

    @Override
    public Mf178TradeResponse couponTrade(Mf178SupplyGood good, Mf178Order order) {
        Mf178SubmitOrder mf178SubmitOrder = JSONObject.parseObject(order.getRawRequest(), Mf178SubmitOrder.class);

        Map<String, String> body = new HashMap<>();
        body.put("outOrderNo", mf178SubmitOrder.getUser_order_id() + "");
        body.put("buyNumber", mf178SubmitOrder.getNum() + "");
        body.put("packageCode", good.getExternalCode());

        Map<String, String> header = new HashMap<>();
        header.put("userName", userName);
        header.put("password", password);
        String url = baseUrl + "/api/merchant/pickUpGoodsByCode";

        String result = Mf178Utils.doPostFormUrlEncoded(url, header, body);

        mf178OrderService.setLog(mf178SubmitOrder.getUser_order_id() + "", "header----" + JSONObject.toJSONString(header) + "----body----" + JSONObject.toJSONString(body), result);

        JSONObject resultJson = JSONObject.parse(result);
        if (resultJson == null || resultJson.getInteger("code") == null) {
            return Mf178TradeResponse.error(999, "接口调用失败请查看日志");
        }
        if (!resultJson.getInteger("code").equals(200)) {
            return Mf178TradeResponse.error(resultJson.getInteger("code"), resultJson.getString("msg"));
        }
        JSONArray datas = resultJson.getJSONArray("data");
        List<Mf178TradeResponse.Mf178TradeResponseData> list = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            JSONObject data = datas.getJSONObject(i);
            Mf178TradeResponse.Mf178TradeResponseData tradeResponseData = new Mf178TradeResponse.Mf178TradeResponseData();
            tradeResponseData.setOrderNo(data.getString("orderNo"));
            tradeResponseData.setCardPwd(data.getString("sign"));
            tradeResponseData.setCostPrice(data.getBigDecimal("productAmount"));
            tradeResponseData.setCardType("3");
            list.add(tradeResponseData);
        }
        return Mf178TradeResponse.success(list);
    }

    @Override
    public String cardStatus(Mf178Order order) {
        return null;
    }

    @Override
    public String dropCard(Mf178Order order) {
        return null;
    }

    public String merchantPackageInfo(String externalCode){
        String url = baseUrl + "/api/myx/base/package/merchantPackageInfo?merchantId="+userName+"&packageCode="+externalCode;
        String result = HttpUtils.sendGet(url);
        return result;
    }
}
